package prj.carmap.mobile.process;

import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.activity.ProfileActivity;
import prj.carmap.mobile.model.UserInfo;
import prj.carmap.mobile.util.ViewUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * 修改密码请求处理
 *
 */
public class ChangePasswdProcess extends AsyncHttpResponseHandler implements OnClickListener {

	private static AsyncHttpClient client = new AsyncHttpClient();
	private EditText txtOriginalPasswd;
	private EditText txtNewPasswd;
	private EditText txtNewPasswdAgain;
	private Button btnChange;

	private Context context;

	@Override
	public void onClick(View v) {
		switch (v.getId())
		{
			case R.id.btnChange:
				//			context.startActivity(new Intent(context,MainActivity.class));
				//			((Activity)context).finish();
				changePasswd();
				break;
			default:
				break;
		}
	}

	private void changePasswd() {
		//JSONObject jsonParams = new JSONObject();
		Log.i("sss", "123");
		String originalPasswd = txtOriginalPasswd.getText().toString().trim();
		String newPassword = txtNewPasswd.getText().toString().trim();
		String newPasswdAgain = txtNewPasswdAgain.getText().toString().trim();
		String cardNo = CarmapApplication.getInstance().userInfo.cardNo;
		
		if (!isNetworkConnected()) {
			ViewUtil.makeToast(context, R.string.error_net_connect);
			return;
		}
		else if ("".equals(originalPasswd)) {
			Log.i("sss", "rrr");
			ViewUtil.makeToast(context, R.string.error_empty_old_password);
			return;
		}
		else if ("".equals(newPassword)) {
			Log.i("sss", "ttt");
			ViewUtil.makeToast(context, R.string.error_empty_new_password);
			return;
		}
		else if ("".equals(newPasswdAgain)) {
			Log.i("sss", "yyy");
			ViewUtil.makeToast(context, R.string.error_empty_new_password_again);
			return;
		}
		else if (!newPasswdAgain.equalsIgnoreCase(newPassword)) {
			Log.i("sss", "uuu");
			ViewUtil.makeToast(context, R.string.error_empty_repeat_password_not_equal);
			return;
		}

		else
		{
			btnChange.setEnabled(false);
			client.get(CarmapApplication.SERVER_URL + "/rest/updatepsw/" + URLEncoder.encode(cardNo) + "/" + URLEncoder.encode(originalPasswd) + "/" + URLEncoder.encode(newPassword), this);
		}
	}

	/**
	 * Fired when a request returns successfully, override to handle in your own code
	 * @param content the body of the HTTP response from the server
	 * @throws JSONException
	 */
	public void onSuccess(String content) {
		try
		{
			JSONObject json = new JSONObject(content);
			if (json.getString("cardNo") != "null")
			{
				//登录成功
//				UserInfo userInfo = new UserInfo();
//				userInfo.cardNo = json.getString("cardNo");
//				userInfo.phone = json.getString("telephone");
//				userInfo.name = json.getString("name");
//				CarmapApplication.getInstance().isLogin = true;
//				CarmapApplication.getInstance().userInfo = userInfo;
				ViewUtil.makeToast(context, R.string.change_passwd_success);
				context.startActivity(new Intent(context, ProfileActivity.class));
				((Activity) context).finish();
			}
			else
			{
				ViewUtil.makeToast(context, R.string.error_change_password);
				btnChange.setEnabled(true);
			}
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Fired when a request fails to complete, override to handle in your own code
	 * @param error the underlying cause of the failure
	 * @param content the response body, if any
	 */
	public void onFailure(Throwable error, String content) {
		ViewUtil.makeToast(context, R.string.error_net_connect);
		btnChange.setEnabled(true);
	}


	public boolean isNetworkConnected() {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager == null) {
			return false;
		}

		NetworkInfo[] infos = manager.getAllNetworkInfo();
		for (NetworkInfo info : infos) {
			if (info.isConnected()) {
				return true;
			}
		}

		return false;
	}



	public EditText getTxtOriginalPasswd() {
		return txtOriginalPasswd;
	}

	public void setTxtOriginalPasswd(EditText txtOriginalPasswd) {
		this.txtOriginalPasswd = txtOriginalPasswd;
	}

	public EditText getTxtNewPasswd() {
		return txtNewPasswd;
	}

	public void setTxtNewPasswd(EditText txtNewPasswd) {
		this.txtNewPasswd = txtNewPasswd;
	}

	public EditText getTxtNewPasswdAgain() {
		return txtNewPasswdAgain;
	}

	public void setTxtNewPasswdAgain(EditText txtNewPasswdAgain) {
		this.txtNewPasswdAgain = txtNewPasswdAgain;
	}

	public Button getBtnChange() {
		return btnChange;
	}

	public void setBtnChange(Button btnChange) {
		this.btnChange = btnChange;
	}

	public void setContext(Context context) {
		this.context = context;
	}

}
